﻿using AiXiu.Common;
using AiXiu.DAL;
using AiXiu.IBLL;
using AiXiu.IDAL;
using AiXiu.Model;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AiXiu.BLL
{
    /// <summary>
    /// 视频业务管理类
    /// </summary>
    public class VideoManager : IVideoManager
    {

        /// <summary>
        /// 待上传视频
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <param name="fileName">文件名</param>
        /// <param name="headline">标题名</param>
        /// <param name="location">位置</param>
        public CreateUploadVideoResult ToBeUpload(int userId, string fileName, string headline, string location = null)
        {
            //1.调用方法获取上传凭证
            VodHelper vodHelper = new VodHelper();       
            CreateUploadVideoResult result= vodHelper.CreateUploadVideo(headline, fileName);

            //2、视频信息保存到数据库
            IVideoService videoService = new VideoService();
            TBVideos video = new TBVideos();
            video.VideoId = result.VideoId;
            video.UserId = userId;
            video.Headline = headline;
            video.Location = location;
            videoService.AddVideo(video);

            return result;
        }

        /// <summary>
        /// 更新视频，返回本次更新的数量
        /// </summary>
        /// <returns></returns>
        public int UpdateVideos()
        {
            //调用DAL层方法——获取处理中的视频Id列表
            IVideoService videoService = new VideoService();
            List<string> videoIds= videoService.GetInProcessVideoIds();
            if (videoIds.Count > 0)
            {
                //	调用VodHelper中【批量获取视频信息_阿里云平台的视频信息】的方法，获取视频信息结果类GetVideoInfosResult
                VodHelper vodHelper = new VodHelper();
                GetVideoInfosResult result=vodHelper.GetVideoInfos(videoIds);
                //调用DAL层方法——批量更新视频信息
                videoService.UpdateVideos(result.VideoList);
                return videoIds.Count;
            }
            else
            {
                return 0;
            }

        }

        /// <summary>
        /// 根据用户id获取视频列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TBVideos> GetVidesByUserId(int userId)
        {
            IVideoService videoService = new VideoService();
            return videoService.GetVideosByUserId(userId);
        }

        /// <summary>
        /// 根据视频id删除视频
        /// </summary>
        /// <param name="videoId"></param>
        public void DeleteVideo(string videoId)
        {
            //从数据删除视频
            IVideoService videoService = new VideoService();
            videoService.DeleteVideo(videoId);

            //从阿里云删除视频
            VodHelper vodHelper = new VodHelper();
            List<string> videoIds = new List<string>();
            videoIds.Add(videoId);
            vodHelper.DeleteVideos(videoIds);
        }

        /// <summary>
        /// 获取视频列表
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            IVideoService videoService = new VideoService();
            return videoService.GetVideos();
        }

        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            IVideoService videoService = new VideoService();
            TBVideos video = videoService.GetVideoByVideoId(videoId);
            return video;
        }


        /// <summary>
        /// 初始化播放
        /// </summary>
        /// <param name="videoId">视频Id</param>
        /// <returns></returns>
        public GetPlayInfoResult InitPlay(string videoId)
        {
            VodHelper vodHelper = new VodHelper();
            GetPlayInfoResult getResult = vodHelper.GetPlayInfo(videoId);
            return getResult;
        }
    }
}